<?php
namespace jy_doudian;
defined('BY_JYA') or exit('error');
/*
	商品接口
	https://op.jinritemai.com/docs/api-docs/14/2004
*/
class goods extends \plugin{
	/*
		添加商品
	*/
	public function add($goodsid,$post=array()){
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid));
		if(!$goods['id']){
			return false;
		}
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');		
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$item['oldspecs']=$item['specs'];
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
		$goods['skus']=array();
		if($option_list){
			foreach($option_list as $k=>&$i){
				$goods['skus'][$k]=array(
					'out_product_id'=>$goods['id'],
					'out_sku_id'=>$i['id'],
					'thumb_img'=>$i['thumb'],
					'sale_price'=>$i['price']*100,
					'market_price'=>$i['costprice']*100,
					'stock_num'=>$i['stock'],
					'barcode'=>$i['goodssn'],
					'sku_code'=>$i['productsn'],
				);
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$goods['skus'][$k]['sku_attrs'][]=array(
						'attr_key'=>$spec_title,
						'attr_value'=>$op['title'],
					);
				}
			}
		}else{
			$goods['skus'][]=array(
				'out_product_id'=>$goods['id'],
				'out_sku_id'=>$goods['id'],
				'thumb_img'=>$goods['image'],
				'sale_price'=>$goods['price']*100,
				'market_price'=>$goods['marketprice']*100,
				'stock_num'=>$goods['total'],
				'barcode'=>$goods['goodssn'],
				'sku_code'=>$goods['productsn'],
			);
		}
		$goods['api_data']=$post['api_data']?:json_decode($goods['api_data'],true);
		$goods['api_data']['cats']=array(
			array('cat_id'=>6033,'level'=>1),
			array('cat_id'=>6057,'level'=>2),
			array('cat_id'=>6091,'level'=>3),
		);
		$goods['images']=json_decode($goods['images'],true);
		$res=$this->model('app_api')->add_goods($goods);
		$goods['api_data']['plugin']='jy_doudian';
		$update=array(
			'api_data'=>ijson_encode($goods['api_data']),
		);
		pdo_update('jy_weishop_goods',$update,array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function upcate(){
		$cat_list=$this->model('app_api')->get_category();
		foreach($cat_list as $item){
			$isadd=pdo_get('jy_doudian_cate',array('cat_id'=>$item['third_cat_id'],'uniacid'=>$_SESSION['uniacid']));
			$data=array(
				'title'=>trim($item['third_cat_name']),
				'cat_id'=>intval($item['third_cat_id']),
				'f_cat_id'=>intval($item['first_cat_id']),
				'f_cat_name'=>trim($item['first_cat_name']),
				'second_cat_id'=>intval($item['second_cat_id']),
				'second_cat_name'=>trim($item['second_cat_name']),
				'qualification'=>trim($item['qualification']),
				'qualification_type'=>intval($item['qualification_type']),
				'product_qualification'=>trim($item['product_qualification']),
				'product_qualification_type'=>intval($item['product_qualification_type']),
				'wxapp_id'=>$_SESSION['wxapp_id'],
			);
			$id=intval($isadd['id']);
			if($id){
				continue;
				pdo_update('jy_doudian_cate',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_doudian_cate',$data);
			}
		}
		return true;
	}
	public function upgroup(){
		$cat_list=$this->model('app_api')->get_shopcat();
		if(!$cat_list){
			exi('请先前往抖店--创建分类！','error');
		}
		foreach($cat_list as $item){
			$isadd=pdo_get('jy_doudian_group',array('cat_id'=>$item['cat_id'],'uniacid'=>$_SESSION['uniacid']));
			$data=array(
				'title'=>trim($item['name']),
				'cat_id'=>intval($item['cat_id']),
				'f_cat_id'=>intval($item['f_cat_id']),
				'cat_level'=>intval($item['cat_level']),
				'wxapp_id'=>$_SESSION['wxapp_id'],
			);
			$id=intval($isadd['id']);
			if($id){
				pdo_update('jy_doudian_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_doudian_group',$data);
			}
		}
		return true;
	}
	public function up_brand(){
		$brands=$this->model('app_api')->get_brands();
		foreach($brands as $item){
			$data=array(
				'brand_info'=>ijson_encode($item['brand_info']),
			);
			pdo_update('jy_doudian_cate',$data,array('cat_id'=>$item['third_cat_id'],'f_cat_id'=>$item['first_cat_id'],'second_cat_id'=>$item['second_cat_id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function get_brand($api_data=array()){
		$brand_info=pdo_getcolumn('jy_doudian_cate',array('cat_id'=>$api_data['cat_id'],'f_cat_id'=>$api_data['cate_1'],'second_cat_id'=>$api_data['cate_2'],'uniacid'=>$_SESSION['uniacid']),'brand_info');
		$brand_info=json_decode($brand_info,true);
		$brand_id=$brand_info['brand_id']?:0;
		return $brand_id;
	}
	//获取所有商品，多余的删除
	public function up_allgoods(){
		$allgoods=$this->model('app_api')->get_allgoods();
		foreach($allgoods as $item){
			if(!$item['out_product_id']){
				continue;
			}
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['out_product_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$goods['id']){
				$this->model('app_api')->del_goods($item['out_product_id']);
			}
		}
		return true;
	}
}